package info.empathymobile.forecast.testenv.database;

import info.empathymobile.forecast.testenv.R;
import info.empathymobile.forecast.testenv.beans.TestBaseData;
import info.empathymobile.forecast.testenv.beans.TestLog;

import java.sql.SQLException;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.RuntimeExceptionDao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

/**
 * LocalDBConnection helper class used to manage the creation and upgrading of your
 * database. This class also usually provides the DAOs used by the other
 * classes.
 */
public class DatabaseHelper extends OrmLiteSqliteOpenHelper {

	// name of the database file
	private static final String DATABASE_NAME = "empathyforecasttestenv.db";
	// database VERSION
	private static final int DATABASE_VERSION = 1;

	// the DAO objects we use to access the table
	private Dao<TestBaseData, Integer> testBaseDataDao = null;
	private RuntimeExceptionDao<TestBaseData, Integer> testBaseDataRuntimeDao = null;
	private Dao<TestLog, Integer> testLogDao = null;
	private RuntimeExceptionDao<TestLog, Integer> testLogRuntimeDao = null;
	
	public DatabaseHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION, R.raw.ormlite_config);
	}

	/**
	 * This is called when the database is first created. Usually you should
	 * call createTable statements here to create the tables that will store
	 * your data.
	 */
	@Override
	public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) {
		try {
			TableUtils.createTable(connectionSource, TestBaseData.class);
			TableUtils.createTable(connectionSource, TestLog.class);
		} catch (SQLException e) {
			Log.e(DatabaseHelper.class.getName(), "Can't create database", e);
			throw new RuntimeException(e);
		}
	}

	/**
	 * This is called when your application is upgraded and it has a higher
	 * VERSION number. This allows you to adjust the various data to match the
	 * new VERSION number.
	 */
	@Override
	public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource,
			int oldVersion, int newVersion) {
		try {
			TableUtils.dropTable(connectionSource, TestBaseData.class, true);
			TableUtils.dropTable(connectionSource, TestLog.class, true);
			// after we drop the old databases, we create the new ones
			onCreate(db, connectionSource);
		} catch (SQLException e) {
			Log.e(DatabaseHelper.class.getName(), "Can't drop databases", e);
			throw new RuntimeException(e);
		}
	}

	/**
	 * Returns the LocalDBConnection Access Object (DAO) for our TestBaseData class. It
	 * will create it or just give the cached value.
	 */
	public Dao<TestBaseData, Integer> getTestBaseDataDao() throws SQLException {
		if (testBaseDataDao == null) {
			testBaseDataDao = getDao(TestBaseData.class);
		}
		return testBaseDataDao;
	}

	/**
	 * Returns the RuntimeExceptionDao (LocalDBConnection Access Object) VERSION of a Dao
	 * for our TestBaseData class. It will create it or just give the cached
	 * value. RuntimeExceptionDao only through RuntimeExceptions.
	 */
	public RuntimeExceptionDao<TestBaseData, Integer> getTestBaseDataRuntimeDao() {
		if (testBaseDataRuntimeDao == null) {
			testBaseDataRuntimeDao = getRuntimeExceptionDao(TestBaseData.class);
		}
		return testBaseDataRuntimeDao;
	}
	
	/**
	 * Returns the LocalDBConnection Access Object (DAO) for our TestLog class. It
	 * will create it or just give the cached value.
	 */
	public Dao<TestLog, Integer> getTestLogDao() throws SQLException {
		if (testLogDao == null) {
			testLogDao = getDao(TestLog.class);
		}
		return testLogDao;
	}

	/**
	 * Returns the RuntimeExceptionDao (LocalDBConnection Access Object) VERSION of a Dao
	 * for our TestLog class. It will create it or just give the cached
	 * value. RuntimeExceptionDao only through RuntimeExceptions.
	 */
	public RuntimeExceptionDao<TestLog, Integer> getTestLogRuntimeDao() {
		if (testLogRuntimeDao == null) {
			testLogRuntimeDao = getRuntimeExceptionDao(TestLog.class);
		}
		return testLogRuntimeDao;
	}

	/**
	 * Close the database connections and clear any cached DAOs.
	 */
	@Override
	public void close() {
		super.close();
		testBaseDataDao = null;
		testBaseDataRuntimeDao = null;
		testLogDao = null;
		testLogRuntimeDao = null;
	}
}
